package com.sense.kukoo.biz_media.adapter

import com.sense.kukoo.biz_media.adapter.provider.MediaHistoryItemProvider
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.repo.biz.MediaHistoryItemBean
import com.sense.kukoo.common.wiget.adapter.MultiItemAdapter

class MediaHistoryAdapter(
    data: ArrayList<MediaHistoryItemBean>,
    val listener: MediaHistoryEventListener,
): MultiItemAdapter<MediaHistoryItemBean>(data)  {

    private val TAG: String = javaClass.simpleName

    private var mCurrentMusic: LocalMediaListItemBean? = null

    private val mMediaHistoryItemProvider = MediaHistoryItemProvider(listener)

    init {
        addItemProvider(mMediaHistoryItemProvider)
    }

    interface MediaHistoryEventListener{
        fun onMediaPlayClick(bean: MediaHistoryItemBean)
        fun onMediaDelete(bean:MediaHistoryItemBean)
    }

    override fun getItemType(data: MutableList<MediaHistoryItemBean>, position: Int): Int {
        return data.get(position).type
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
        mMediaHistoryItemProvider.setCurrentMusic(music)
        notifyDataSetChanged()
    }

    fun setCurrentPlayState(state: Int, progress: Int){
        mMediaHistoryItemProvider.setCurrentPlayState(state, progress)
        var position = -1
        list.forEachIndexed { index, mediaItemBean ->
            if(mediaItemBean.data.contentId == mCurrentMusic?.idOfChapter()){
                position = index
            }
        }
        if (position==-1) {
            notifyDataSetChanged()
        }else{
            notifyItemChanged(position)
        }

    }
}